package com.c212.manager.filter;

import com.c212.manager.constants.Consts;
import com.c212.manager.utils.annotation.NotLogin;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.lang.Nullable;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean flag = true;
//        if (handler instanceof HandlerMethod) {
//            HandlerMethod handlerMethod = (HandlerMethod) handler;
//            NotLogin login = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), NotLogin.class);
//            if (login == null) {
//                HttpSession session = request.getSession();
//                if (session.getAttribute(Consts.USER_SESSION) == null) {
//                    flag = false;
////                    throw new UserNotLoginException("用户未登录");
//                    throw new Exception("用户未登录");
//                }
//            }
//        }
        return flag;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {

    }
}
